import type { ShapeType } from '@/interface';
import { DEFAULTLAYER } from '@/constant';

interface PropsType {
  startX?: number;
  startY?: number;
}

class Circle implements ShapeType {
  private type: string;
  private layer: number;
  startX: number;
  startY: number;
  endX: number;
  endY: number;
  constructor(props?: PropsType) {
    this.type = 'circle';
    this.layer = DEFAULTLAYER;
    this.startX = props?.startX || 0;
    this.startY = props?.startY || 0;
    this.endX = this.startX;
    this.endY = this.startY;
  }

  get minX(): number {
    return Math.min(this.startX, this.endX);
  }

  get minY(): number {
    return Math.min(this.startY, this.endY);
  }

  get maxX(): number {
    return Math.max(this.startX, this.endX);
  }

  get maxY(): number {
    return Math.max(this.startY, this.endY);
  }

  get radiusX(): number {
    return (this.endX - this.startX) / 2;
  }

  get radiusY(): number {
    return (this.endY - this.startY) / 2;
  }

  get centerX(): number {
    return this.startX + this.radiusX;
  }

  get centerY(): number {
    return this.startY + this.radiusY;
  }

  getType(): string {
    return this.type;
  }

  getLayer(): number {
    return this.layer;
  }

  setEnd(x: number, y: number): void {
    this.endX = x;
    this.endY = y;
  }

  changePosition(diffX: number, diffY: number) {
    this.startX += diffX;
    this.startY += diffY;
    this.endX += diffX;
    this.endY += diffY;
  }

  inside(x: number, y: number) {
    if (x > this.minX && x < this.maxX && y > this.minY && y < this.maxY) {
      return true;
    }
    return false;
  }

  draw(ctx: CanvasRenderingContext2D): void {
    const centerX = Math.abs(this.centerX);
    const centerY = Math.abs(this.centerY);
    const radiusX = Math.abs(this.radiusX);
    const radiusY = Math.abs(this.radiusY);
    ctx.beginPath();
    ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, 2 * Math.PI);
    ctx.stroke();
  }
}

export default Circle;
